package cn.edu.cxcyzx.controller;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import cn.edu.cxcyzx.conf.Role;
import cn.edu.cxcyzx.entity.Check_JSON;
import cn.edu.cxcyzx.entity.Project;
import cn.edu.cxcyzx.entity.ProjectCheck;
import cn.edu.cxcyzx.entity.Room_use;
import cn.edu.cxcyzx.entity.Room_usePOJO;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.supervisor.ServiceFit;
import cn.edu.cxcyzx.util.MsgJSON;
import cn.edu.cxcyzx.util.Room_useInfo;
import cn.edu.cxcyzx.util.Sort_projects;

@Controller
@RequestMapping("/admin_stu")
public class Admin_stuController {
	
	@Resource
	ServiceFit serviceFit;
	Role r=Role.ADMIN;
	
	@RequestMapping("home")
	public ModelAndView HomePage(HttpSession session,@RequestParam(required=false)Date date){
		System.out.println("首页");
		ModelAndView modelAndView =new ModelAndView("admin_stu/admin_index");
		Date lastDate=serviceFit.getRoom_useService().getLastDate();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String firstDate=format.format(serviceFit.getProjectService().getMin_analyseDate());
		System.out.println("lastDate:"+lastDate);
		System.out.println("firstDate:"+firstDate);
		if(date==null){
			//默认查询的日期 为上一次录入数据的日期
			date=lastDate;
		}
		System.out.println("lastDate:"+lastDate);
		ResultDo<Map<Integer,Room_usePOJO[]>> resultDo=serviceFit.getRoom_useService().getRoom_uses(date);
		if(resultDo.isSuccess()){
			System.out.println("success");
			modelAndView.addObject("recordes", resultDo.getResult());
		}
		modelAndView.addObject("lastDate", lastDate);
		modelAndView.addObject("firstDate", firstDate);
		return modelAndView;
	}
	
	@RequestMapping("/getRecordes")
	@ResponseBody
	public MsgJSON getRecordes(String date){
		Date select_date;
		if(date==null||"".equals(date)){
			select_date=serviceFit.getRoom_useService().getLastDate();

		}else{
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			try {
				
				select_date=new Date(format.parse(date).getTime());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return MsgJSON.fail();
			}
		}
		ResultDo<Map<Integer,Room_usePOJO[]>> resultDo=serviceFit.getRoom_useService().getRoom_uses(select_date);
		if(resultDo.isSuccess()){
			return MsgJSON.success().add("recordes", resultDo.getResult());
		}else{
			return MsgJSON.fail();
		}
		
	}
	/**
	 * 查询所有使用房间的项目
	 * @return
	 */
	@RequestMapping("/click_addBtn")
	@ResponseBody
	public MsgJSON click_addBtn(String check_date){
		System.out.println("check_date:"+check_date);
		ResultDo<List<Project>> resultDo=serviceFit.getProjectService().getPro_going(check_date);
		if(resultDo.isSuccess()){
			List<Project> list=resultDo.getResult();
			//按房间号排序
			Sort_projects.sort_roomId_asc(list);
			for (Project project : list) {
				System.out.println(project.getRoomId());
				
			}
			return MsgJSON.success().add("list", list);
		}else{
			return MsgJSON.fail();
		}
	}
	
	@RequestMapping("/add_checkInfo")
	@ResponseBody
	public MsgJSON add_checkInfo(@RequestBody Check_JSON data){
		System.out.println("into");
		System.out.println(data.getPros().get(0).toString());
		System.out.println(data.getDate());
		if(data!=null){
			serviceFit.getRoom_useService().insert_record(data);
			return MsgJSON.success();
		}else{
			return MsgJSON.fail();
		}		
	}
	/**
	 * 重新登陆
	 * @param session
	 * @return
	 */
	@RequestMapping("/relogin")
	@ResponseBody
	public MsgJSON reLogin(HttpSession session){
		session.removeAttribute("admin_stu");
		return MsgJSON.success();
	}
	/**
	 * 退出
	 * @param session
	 * @return
	 */
	@RequestMapping("quit")
	public String quit(HttpSession session){
		session.removeAttribute("admin_stu");
		return "redirect:/login.jsp";
	}
	
}
